package cn.lili.controller.promotion;

import cn.lili.common.exception.ServiceException;
import cn.lili.common.utils.ResultUtil;
import cn.lili.common.vo.PageVO;
import cn.lili.common.vo.ResultMessage;
import cn.lili.modules.promotion.entity.dos.Ticket;
import cn.lili.modules.promotion.entity.dos.TicketArea;
import cn.lili.modules.promotion.entity.dos.TicketCanvas;
import cn.lili.modules.promotion.entity.dto.search.TicketExportParams;
import cn.lili.modules.promotion.entity.vos.TicketAddVO;
import cn.lili.modules.promotion.entity.vos.TicketBatchDownStateVO;
import cn.lili.modules.promotion.entity.vos.TicketBatchSaleVO;
import cn.lili.modules.promotion.service.TicketAreaService;
import cn.lili.modules.promotion.service.TicketCanvasService;
import cn.lili.modules.promotion.service.TicketService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@RestController
@Api(tags = "门票座位接口")
@RequestMapping("/promotion/ticket")
@RequiredArgsConstructor
public class TicketController {
    private final TicketService ticketService;

    private final TicketCanvasService ticketCanvasService;

    private final TicketAreaService ticketAreaService;

    @ApiOperation(value = "获取门票区域列表")
    @GetMapping("/getAreaList")
    public ResultMessage<List<TicketArea>> getAreaList() {
        return ResultUtil.data(ticketAreaService.getList());
    }

    @ApiOperation(value = "获取门票画布列表")
    @GetMapping("/getCanvasList")
    public ResultMessage<List<TicketCanvas>> getCanvasList() {
        return ResultUtil.data(ticketCanvasService.list());
    }

    @ApiOperation(value = "分页查询门票座位")
    @GetMapping
    public ResultMessage<Page<Ticket>> getPage(Ticket queryParam, PageVO page) {
        return ResultUtil.data(ticketService.getByPage(queryParam, page));
    }

    @ApiOperation(value = "生成座位号")
    @PostMapping("/generateTicket")
    public ResultMessage<Void> generateTicket(@RequestBody TicketAddVO param) {
        ticketService.generateTicket(param);
        return ResultUtil.success();
    }

    @ApiOperation(value = "导出座位号")
    @GetMapping("/exportTicket")
    public ResultMessage<List<Ticket>> exportTicket(TicketExportParams params) {
        return ResultUtil.data(ticketService.exportTicket(params));
    }

    @ApiOperation("导入门票")
    @PostMapping(value = "/importTicket", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public ResultMessage<Void> importTicket(@RequestPart("file") MultipartFile file) throws Exception {
        ticketService.importTicket(file);
        return ResultUtil.success();
    }

    @ApiOperation(value = "添加门票")
    @PostMapping("/updateTicketById")
    public ResultMessage<Void> updateTicketById(@RequestBody Ticket ticket) {
        ticketService.updateTicketById(ticket);
        return ResultUtil.success();
    }

    @ApiOperation(value = "上下架")
    @GetMapping("/updateDownState/{id}")
    public ResultMessage<Void> updateDownState(@PathVariable String id, Integer state) {
        ticketService.updateDownState(id, state);
        return ResultUtil.success();
    }

    @ApiOperation(value = "根据画布查询座位列表")
    @GetMapping("/getByCanvasId")
    public ResultMessage<List<Ticket>> getByCanvasId(String canvasId) {
        return ResultUtil.data(ticketService.getByCanvasId(canvasId));
    }

    @ApiOperation(value = "删除门票")
    @GetMapping("/delete")
    public ResultMessage<Void> delete(String id) {
        ticketService.deleteById(id);
        return ResultUtil.success();
    }

    @ApiOperation(value = "根据画布大小查询座位")
    @GetMapping("/getTicketBySize")
    public ResultMessage<List<Ticket>> getTicketBySize(String areaName, String areaDirection,
                                                       Integer rowStart, Integer columnStart,
                                                       Integer rowEnd, Integer columnEnd) {
        return ResultUtil.data(ticketService.getTicketBySize(areaName, areaDirection, rowStart, columnStart, rowEnd, columnEnd));
    }

    @ApiOperation(value = "根据区域和方向获取画布大小")
    @GetMapping("/getTicketCanvas")
    public ResultMessage<TicketCanvas> getTicketCanvas(String areaName, String areaDirection) {
        return ResultUtil.data(ticketCanvasService.getByAreaName(areaName, areaDirection));
    }

    @ApiOperation(value = "根据Id获取门票信息")
    @GetMapping("/getById/{id}")
    public ResultMessage<Ticket> getById(@PathVariable String id) {
        return ResultUtil.data(ticketService.getById(id));
    }

    @ApiOperation(value = "批量售出门票")
    @PostMapping("/batchSale")
    public ResultMessage<Void> batchSale(@RequestBody TicketBatchSaleVO batchSaleVO) {
        ticketService.batchSale(batchSaleVO);
        return ResultUtil.success();
    }

    @ApiOperation(value = "批量上下架")
    @PostMapping("/batchDownState")
    public ResultMessage<Void> batchDownState(@RequestBody TicketBatchDownStateVO batchDownStateVO) {
        ticketService.batchDownState(batchDownStateVO);
        return ResultUtil.success();
    }
}